<?php
/**
 * 同城微聚汇
 * ============================================================================
 * 版权所有 2015-2027 武汉鼎联信息技术有限公司，并保留所有权利。
 * 网站地址: http://dlshop.dliansoft.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃      
 * Date: 2015-09-09
 */

namespace app\admin\controller;
use think\Db;
use think\Page;

class Wemsg extends Base {


    public function index(){
        return $this->fetch('index1');
    }

    public function ajaxindex(){
        $count = M('wx_template_msg')->count();
        $page = new Page($count, 20);
        $userList = M('wx_template_msg')->limit($page->firstRow,$page->listRows)->select();
        foreach($userList as $k=>$v){
            $admin = M('admin')->field('user_name')->where('admin_id',$v['admin_id'])->find();
            $userList[$k]['admin_id'] = $admin['user_name'];
            $template = M('wx_template')->field('sms_sign')->where('tpl_id',$v['template'])->find();
            $userList[$k]['template'] = $template['sms_sign'];
        }
        $this->assign('page',$page->show());
        $this->assign('pager',$page);
        $this->assign('userList',$userList);
        return $this->fetch();
    }

    public function detail(){
        $id = I('id');
        $where['t_id'] = $id;
        $count = M('wx_template_msg_list')->where($where)->count();
        $page = new Page($count, 20);
        $userList = M('wx_template_msg_list')->where($where)->limit($page->firstRow,$page->listRows)->select();
        foreach($userList as $k=>$v){
            if($v['status'] == 'ok'){
                $userList[$k]['status'] = '发送成功';
            }else{
                $userList[$k]['status'] = '发送失败';
            }
            if($v['error_log'] == '0'){
                $userList[$k]['error_log'] = '';
            }
            $user = M('users')->field('nickname')->where('user_id',$v['to_member'])->find();
            $userList[$k]['to_member'] = $user['nickname'];
        }
        $this->assign('page',$page->show());
        $this->assign('pager',$page);
        $this->assign('userList',$userList);
        return $this->fetch();
    }

    public function add_msg(){
        $id = I('id');
        if($id){
            $msg = M('wx_template_msg')->find($id);
            $this->assign('msg',$msg);
        }
        $template = M('wx_template')->select();
        $this->assign('template',$template);
        return $this->fetch();
    }

    public function add_info(){
        $admin = getAdminId();
        $add = $_POST;
        $add['admin_id'] = $admin['admin_id'];
        $add['suppliers_id'] = $admin['suppliers_id'];
        $add['createtime'] = time();
        $id = $add['id'];
        if($add){
            if($id){
                $re = M('wx_template_msg')->where('id',$id)->save($add);
            }else{
                $re = M('wx_template_msg')->add($add);
            }
            if($re){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'data'  => array('url'=>U('Admin/Wemsg/index')),
                );
            }else {
                $return_arr = array(
                    'status' => -1,
                    'msg'   => '操作失败',
                    'data'  => '操作失败',
                );
            }
            $this->ajaxReturn($return_arr);
        }
    }

    public function delete(){
        $id = I('id');
        if($id){
            $re = M('wx_template_msg')->delete($id);
            if($re){
                $this->success('删除成功');
            }else {
                $this->error('删除失败');
            }
        }
    }

}